import {
  Body,
  Controller,
  Delete,
  Get,
  HttpStatus,
  Param,
  Post,
  Put,
  Query,
} from '@nestjs/common';
import { PlateService } from './plate.service';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { MessagePattern } from '@nestjs/microservices';
import { RequireLogin } from '@app/auth/utils/guard';
import { PublicVoError, PublicVoSuccess } from '@app/utils/publicVo';
import { CreatePlateDto } from './dto/create-plate.dto';
import { UpdatePlateDto } from './dto/update-plate.dto';
import { PlateListVo } from './vo/plate-list.vo';
@ApiTags('用户系统-板块中心')
@Controller('plate')
export class PlateController {
  constructor(private readonly plateService: PlateService) {}

  /**
   * TCP 模块
   */
  @MessagePattern('get-plate')
  async getPlateListTCP(data: any) {
    const { pageNum, pageSize, status } = data.query;
    return await this.plateService.list(status, pageNum, pageSize);
  }
  @MessagePattern('post-plate')
  postPlateListTCP(data: any) {
    return this.plateService.create(data.body);
  }

  @MessagePattern('put-plate')
  putPlateListTCP(data: any) {
    return this.plateService.update(data.body);
  }

  @MessagePattern('delete-plate')
  deletePlateListTCP(data: any) {
    return this.plateService.romove(data.params.id);
  }
  /**
   * HTTP 模块
   */
  @Get()
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({
    summary: '业务板块列表',
    description: '业务板块（模块）列表接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PlateListVo,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  @ApiQuery({
    name: 'status',
    type: String,
    description: '状态',
    required: false,
  })
  @ApiQuery({
    name: 'pageNum',
    type: Number,
    description: '页码',
    required: false,
  })
  @ApiQuery({
    name: 'pageSize',
    type: Number,
    description: '页数',
    required: false,
  })
  async getPlateList(
    @Query('pageNum') pageNum?: number,
    @Query('pageSize') pageSize?: number,
    @Query('status') status?: string,
  ) {
    return await this.plateService.list(status, pageNum, pageSize);
  }

  @Post()
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({
    summary: '业务板块新增',
    description: '业务板块（模块）新增接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async postPlateList(@Body() createPlateDto: CreatePlateDto) {
    return this.plateService.create(createPlateDto);
  }

  @Put()
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({
    summary: '业务板块修改',
    description: '业务板块（模块）修改接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async putPlateList(@Body() updatePlateDto: UpdatePlateDto) {
    return this.plateService.update(updatePlateDto);
  }

  @Delete(':id')
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({
    summary: '业务板块删除',
    description: '业务板块（模块）删除接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  deletePlateList(@Param('id') id: number) {
    return this.plateService.romove(id);
  }
}
